package impl;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;

import baise.BaseDaoImpl;
import baise.iHibBaseDAO;
import baise.iHibBaseDAOimpl;
import model.TBook;
import model.TUser;
import DAO.UserDAO;
/**
 * 用户实现类
 * @author Administrator
 *
 */
public class UserDAOimpl implements UserDAO {
	iHibBaseDAO iBaseDAO=new iHibBaseDAOimpl();
	@Override
	public boolean addUserall(TUser tUser) {
		// TODO Auto-generated method stub
		return (Boolean) iBaseDAO.insert(tUser);
	}

	@Override
	public Object addUser(TUser tUser) {
		// TODO Auto-generated method stub
		return iBaseDAO.insert(tUser);
	}

	@Override
	public boolean deleteUser(TUser tUser) {
		// TODO Auto-generated method stub
		return iBaseDAO.delete(tUser);
	}

	@Override
	public boolean updateUser(TUser tUser) {
		
		return iBaseDAO.update(tUser);
	}

	@Override
	public TUser selectUser(int id, String pwd) {
		
		return (TUser) iBaseDAO.findById(TUser.class, id);
	}

	@Override
	public List<TBook> logintUser(int id, String pwd, String phone) throws SQLException {
		BaseDaoImpl bea=new BaseDaoImpl();
		ResultSet rs=null;
		if (pwd==null) {
			String sql="select * from T_User where id="+id+" and phone='"+phone+"'";
			rs=bea.select(sql);
		}if (phone==null) {
			String sql="select * from T_User where id="+id+" and pwd='"+pwd+"'";
			rs=bea.select(sql);
		}
		List<TBook> list=convertList(rs);
		return list;
	}

	/**
	 * ������
	 * @param rs
	 * @return
	 * @throws SQLException
	 */
	
	private static List convertList(ResultSet rs) throws SQLException{
		List list = new ArrayList();
		ResultSetMetaData md = rs.getMetaData();//��ȡ����
		int columnCount = md.getColumnCount();//��ȡ�е�����
		while (rs.next()) {
			Map rowData = new HashMap();//����Map
				for (int i = 1; i <= columnCount; i++) {
					rowData.put(md.getColumnName(i), rs.getObject(i));//��ȡ����ֵ
				}
				list.add(rowData);
			}
		return list;
	}
//	public static void main(String[] args) {
//		UserDAOimpl u=new UserDAOimpl();
//		try {
//			List<TBook> list=u.logintUser(1,"123456",null);
//			//TBook tb=(TBook)list.get(0);
//			String json=JSON.toJSONString(list);
//			//System.out.println(json);
//			//TBook tb=(TBook) JSON.parseObject(json,TBook.class);
//			System.out.println(u.logintUser(1,"123456",null));
//		} catch (SQLException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//
//	}

}
